/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.domain;

import org.hibernate.annotations.Proxy;
import org.xptracker.persistence.Identified;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name="effort_entries")
@Proxy(lazy=false)
public class EffortEntry extends Identified {
  // Temporary holder for serialization until I find a better way
  private transient String userId;

  private User user;
  private Date start;
  private Date end;
  private Story story;
  private String comment;

  public EffortEntry() {
  }

  public EffortEntry(Story story, User user, Date start, Date end, String comment) {
    this.story = story;
    this.user = user;
    this.start = start;
    this.end = end;
    this.comment = comment;
  }

  @ManyToOne(targetEntity=Story.class)
  public Story getStory() {
    return story;
  }

  public void setStory(Story story) {
    this.story = story;
  }

  @Column(name="start_date")
  public Date getStart() {
    return start;
  }

  public void setStart(Date start) {
    this.start = start;
  }

  @Column(name="end_date")
  public Date getEnd() {
    return end;
  }

  public void setEnd(Date end) {
    this.end = end;
  }

  @ManyToOne(targetEntity=User.class)
  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }

  @Transient
  public String getUserId() {
    return userId != null ? userId : user == null ? "" : user.getId();
  }

  public void setUserId(String userId) {
    this.userId = userId;
  }

  public String getComment() {
    return comment;
  }

  public void setComment(String comment) {
    this.comment = comment;
  }

  @Transient
  public long getDuration() {
    if (isCompleted()) {
      long result = end.getTime() - start.getTime();
      return result < 0 ? 0 : result;
    }
    return 0;
  }

  @Transient
  public boolean isCompleted() {
    return this.end != null;  
  }

  public void stop() {
    if (isCompleted()) {
      throw new IllegalStateException("Entry already stopped.");
    }
    setEnd(new Date());

    if (end.before(start)) {
      end = start;
    }
  }

  public boolean equals(Object o) {
    if (getId() != null) {
      return super.equals(o);
    }

    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    if (!super.equals(o)) return false;

    EffortEntry that = (EffortEntry) o;

    return !(end != null ? !end.equals(that.end) : that.end != null) &&
        !(start != null ? !start.equals(that.start) : that.start != null) &&
        !(story != null ? !story.equals(that.story) : that.story != null) &&
        !(user != null ? !user.equals(that.user) : that.user != null);
  }

  public int hashCode() {
    int result = super.hashCode();
    result = 31 * result + (user != null ? user.hashCode() : 0);
    result = 31 * result + (start != null ? start.hashCode() : 0);
    result = 31 * result + (end != null ? end.hashCode() : 0);
    result = 31 * result + (story != null ? story.hashCode() : 0);
    return result;
  }
}
